package com.zx.idc.backend.gui.sys;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.ds.sys.entity.SysKeywordRepo;
import com.zx.idc.ds.sys.service.ISysBaseCodeService;
import com.zx.idc.ds.sys.service.ISysKeywordRepoService;
import com.zx.idc.ds.sys.vo.SysKeywordRepoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 关键词管理 前端控制器
 * </p>
 *
 * @author chendong
 * @since 2019-04-17
 */
@RestController
@RequestMapping("/api/sysKeywordRepo")
public class SysKeywordRepoController extends BasicBaseApi<ISysKeywordRepoService, SysKeywordRepo> implements BaseExcelApi<ISysKeywordRepoService, SysKeywordRepoVo> {

    @Autowired
    private  ISysBaseCodeService iSysBaseCodeService;

    @GetMapping("/query-page")
    @RequiresPermissions("system-configuration:sys-keyword-repo")
    public Result<Page<SysKeywordRepoVo>> page(Page< SysKeywordRepoVo> page, SysKeywordRepoVo sysKeywordRepoVo) {
        return success(service.pageByParam(page, sysKeywordRepoVo));
    }

    /**
     * 根据id删除
     * 实际是更新delFlag字段 为 1：已删除,
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @RequiresPermissions("system-configuration:sys-keyword-repo:edit")
    public Result<Boolean> del(@PathVariable("id") Long id) {
        return success(service.deleteById(id));
    }

    /**
     * 更新关键词组
     * @param id
     * @param category 关键词组编号
     * @return
     */
    @GetMapping("/update_category/{id}/{category}")
    @RequiresPermissions("system-configuration:sys-keyword-repo:edit")
    public Result<Boolean> updateCategory(@PathVariable("id") Long id, @PathVariable("category") Long category) {
        SysKeywordRepo sysKeywordRepo=new SysKeywordRepo();
        sysKeywordRepo.setId(id);
        sysKeywordRepo.setCategory(category);
        return success(service.updateById(sysKeywordRepo));
    }


    @PostMapping("/update-or-add")
    @RequiresPermissions("system-configuration:sys-keyword-repo:edit")
    public Result<Boolean> updateOrAdd(@RequestBody SysKeywordRepo sysKeywordRepo) {
        if (sysKeywordRepo.getId() == null) {
            sysKeywordRepo.setCreateTime(LocalDateTime.now());
        }
        return success(service.updateOrAdd(sysKeywordRepo));
    }

}

